/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kimo.bl;

import java.util.Date;
import kimo.persistence.controllers.exceptions.NonexistentEntityException;
import kimo.persistence.entities.Pcheck;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Grid;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author nektarios
 */
public class CheckUI extends KimoUI {

    @Wire
    Listbox pcheckLstBox;
    ListModelList lmlPcheck;

    @Wire
    Div pcheck_div;

    @Wire
    Grid editableCheckGrid;

    @Wire
    Datebox date_check;

    @Wire
    Textbox check_descr;

    @Wire
    Textbox check_result;

    @Wire
    Textbox check_penalty;

    private boolean new_pcheck;
    private Pcheck pCheck;

    @Override
    void getFromUI() {
        pCheck.setCheckDate(date_check.getValue());
        pCheck.setCheckDescr(check_descr.getValue());
        pCheck.setCheckResult(check_result.getValue());
        pCheck.setCheckPenalty(check_penalty.getValue());
    }

    @Override
    void updateUI() {
        lmlPcheck = new ListModelList<Pcheck>(getKdao().getPchecks());
        pcheckLstBox.setModel(lmlPcheck);
    }

    void updateGridUI() {
        date_check.setValue(pCheck.getCheckDate());
        check_descr.setValue(pCheck.getCheckDescr());
        check_result.setValue(pCheck.getCheckResult());
        check_penalty.setValue(pCheck.getCheckPenalty());
    }

    @Override
    void populateListboxs() {
        lmlPcheck = new ListModelList<Pcheck>(getKdao().getPchecks());
        pcheckLstBox.setModel(lmlPcheck);
    }

    @Override
    void clearUI() {
        date_check.setRawValue("");
        check_descr.setRawValue("");
        check_result.setRawValue("");
        check_penalty.setRawValue("");
    }

    @Listen("onClick = #insertCheckBtn")
    public void onInsertCheckBtnClick() {
        clearUI();
        pcheck_div.setVisible(false);
        editableCheckGrid.setVisible(true);
        date_check.setValue(new Date());
        new_pcheck = true;
    }

    @Listen("onClick = #cancelPCheckBtn")
    public void onCancelPCheckBtnClick() {

        editableCheckGrid.setVisible(false);
        pcheck_div.setVisible(true);
        clearUI();
    }

    @Listen("onClick = #savePCheckBtn")
    public void onSavePCheckBtnClick() throws Exception {
        if (new_pcheck) {
            // εισαγουμε εγγραφη
            pCheck = new Pcheck();
            getFromUI();
            getKdao().saveNewPCheck(pCheck);
            lmlPcheck.add(pCheck);
            Messagebox.show("Η εγγραφή καταχωρήθηκε !");
            editableCheckGrid.setVisible(false);
            pcheck_div.setVisible(true);
        } else {
            getFromUI();
            getKdao().editPCheck(pCheck);
            lmlPcheck.set(lmlPcheck.indexOf(pCheck),pCheck);
            editableCheckGrid.setVisible(false);
            pcheck_div.setVisible(true);
            //show message for user
            Clients.showNotification("Η εγγραφή ενημερώθηκε", 
                    null, pcheckLstBox, "bottom_center", 3000);
        }
    }

    @Listen("onEdit = #pcheckLstBox")
    public void onProductionEdit(ForwardEvent evt) {
        Button btn = (Button) evt.getOrigin().getTarget();
        Listitem litem = (Listitem) btn.getParent().getParent();

        pCheck = (Pcheck) litem.getValue();

        new_pcheck = false;
        pcheck_div.setVisible(false);

        editableCheckGrid.setVisible(true);
        updateGridUI();
    }

    @Listen("onDelete = #pcheckLstBox")
    public void removeProduction(ForwardEvent evt) throws NonexistentEntityException {

        Button btn = (Button) evt.getOrigin().getTarget();
        Listitem litem = (Listitem) btn.getParent().getParent();

        pCheck = (Pcheck) litem.getValue();

        getKdao().deletePcheck(pCheck);

        editableCheckGrid.setVisible(false);
        pcheck_div.setVisible(true);
        Messagebox.show("Η εγγραφή Διαγράφτηκε");
        lmlPcheck.remove(pCheck);
    
    }

}
